package com.ckzp.core.json;

import com.alibaba.fastjson.annotation.JSONField;
import com.ckzp.core.ConfigUtil;
import com.ckzp.core.Results;
import com.ckzp.jfinal.base.BaseController;
import com.jfinal.core.Const;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 51594 on 2022/7/27.
 */
public class JsonResult implements Serializable {

    private static String ERR_91 = "登陆已过期";
    private static String ERR_92 = "无效TOKEN";
    private static String ERR_93 = "没有权限";
    private static String ERR_94 = "您未登陆";
    private static String ERR_99 = "未知错误";


    /**
     *
     */
    public int code = 0;
    /**
     *
     */
    public String msg;
    /**
     *
     */
    public String extra;
    /**
     *
     */
    public Map token;

    /**
     *
     */
    public Object data;


    public static JsonResult n(int codes, String msgs) {
        JsonResult result = new JsonResult();
        switch (codes) {
            case 91:
                msgs = ERR_91;
            case 92:
                msgs = ERR_92;
            case 93:
                msgs = ERR_93;
            case 94:
                msgs = ERR_94;
            case 99:
                msgs = ERR_99;
        }
        result.setCode(codes).setMsg(msgs);
        return result;
    }

    /**
     * @param status
     * @param msgs
     * @return
     */
    public static JsonResult n(boolean status, String msgs) {
        return JsonResult.n().setCode(status ? 0 : 9).setMsg(msgs + (status ? "成功" : "失败"));
    }

    public static JsonResult n() {
        return new JsonResult();
    }

    /**
     * @param codes
     * @param msgs
     * @param controller
     * @return
     */
    public static JsonResult n(int codes, String msgs, BaseController controller) {
        JsonResult result = JsonResult.n().setCode(codes).setMsg(msgs);
        if (ConfigUtil.isToken()) {
            String tokenId = controller.getAttr(Const.DEFAULT_TOKEN_NAME);
            result.setToken(Const.DEFAULT_TOKEN_NAME, tokenId);
        }
        return result;
    }


    /**
     * @param codes
     * @param msgs
     * @return
     */
    public static JsonResult n(int codes, String msgs, String extra) {
        JsonResult result = JsonResult.n().setCode(codes).setMsg(msgs).setExtra(extra);

        return result;
    }

    public static JsonResult n(Results results) {
        return JsonResult.n(results.getCode(), results.getMsg());
    }


    public int getCode() {
        return code;
    }

    public JsonResult setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public JsonResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public Map getToken() {
        return token;
    }

    public JsonResult setData(Object data) {
        this.data = data;
        return this;
    }

    public String getExtra() {
        return extra;
    }

    public JsonResult setExtra(String extra) {
        this.extra = extra;
        return this;
    }

    public JsonResult setToken(String ids, String values) {
        if (this.token != null) this.token.put(ids, values);
        return this;
    }


    /**
     * @return
     */
    public String toJson() {
        return JsonUtil.toJsonString(this);
    }
}
